//
// Created by jiang on 2021/12/30.
//
#include "key.h"
#include "multi_button.h"
#include "motor.h"

struct Button key_up;
struct Button key_down;

// Read Button GPIO
uint8_t read_keyUp_GPIO(void) {
    return HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin);
}

uint8_t read_keyDown_GPIO(void) {
    return HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin);
}


void UP_SINGLE_Click_Handler(void *btn) {
    MOTO_SpeedControl(&motor, 20);
}

void DOWN_SINGLE_Click_Handler(void *btn) {
    MOTO_SpeedControl(&motor, -20);
}

void UP_LONG_PRESS_Handler(void *btn) {
    MOTO_SpeedControl(&motor, 3);
}

void DOWN_LONG_PRESS_Handler(void *btn) {
    MOTO_SpeedControl(&motor, -3);
}

// Button Event Register
void KEY_Init(void) {
    button_init(&key_up, read_keyUp_GPIO, 0);
    button_init(&key_down, read_keyDown_GPIO, 0);

    button_attach(&key_up, SINGLE_CLICK, UP_SINGLE_Click_Handler);
    button_attach(&key_up, LONG_PRESS_HOLD, UP_LONG_PRESS_Handler);

    button_attach(&key_down, SINGLE_CLICK, DOWN_SINGLE_Click_Handler);
    button_attach(&key_down, LONG_PRESS_HOLD, DOWN_LONG_PRESS_Handler);

    button_start(&key_up);
    button_start(&key_down);
}


void KEY_Scan(void) {
    button_ticks();
}

